package com.example.workmanagerdemo;

import android.content.Context;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.work.Data;
import androidx.work.testing.TestWorkerBuilder;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import androidx.work.ListenableWorker.Result;

import static com.example.workmanagerdemo.UploadLogWorker.OUTPUT_DATA;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

/**
 * author: tuyrt7 on 2020/12/11.
 * desc:
 */
@RunWith(AndroidJUnit4.class)
public class UploadLogWorkerJavaTest {

    private Context mContext;
    private Executor mExecutor;

    @Before
    public void setUp() {
        mContext = ApplicationProvider.getApplicationContext();
        mExecutor = Executors.newSingleThreadExecutor();
    }

    @Test
    public void testUploadLogWorker() {
        Data inputData = new Data.Builder()
                .putInt(MainActivity.INPUT_DATA, 12)
                .build();

        UploadLogWorker worker =
                (UploadLogWorker) TestWorkerBuilder.from(mContext,
                        UploadLogWorker.class,
                        mExecutor)
                        .setInputData(inputData)
                        .build();

        Data outputData = new Data.Builder()
                .putString(OUTPUT_DATA, "Task Success!")
                .build();
        Result result = worker.doWork();
        assertThat(result, is(Result.success(outputData)));
    }
}
